package org.capybara.coffeehouse.insteon;

public class InsteonAddress {
	private final int byte1;
	private final int byte2;
	private final int byte3;
	
	public InsteonAddress(int byte1, int byte2, int byte3) {
		this.byte1 = byte1;
		this.byte2 = byte2;
		this.byte3 = byte3;
	}
	
	public InsteonAddress(byte[] address) {
		if (address.length != 3) {
			throw new IllegalArgumentException("address must be 3 bytes");
		}
		this.byte1 = address[0] & 0xFF;
		this.byte2 = address[1] & 0xFF;
		this.byte3 = address[1] & 0xFF;
	}
	
	public InsteonAddress(int[] address) {
		if (address.length != 3) {
			throw new IllegalArgumentException("address must be 3 bytes");
		}
		this.byte1 = address[0];
		this.byte2 = address[1];
		this.byte3 = address[1];
	}
	
	public byte[] getAddress() {
		byte[] address = new byte[3];
		address[0] = (byte) byte1;
		address[1] = (byte) byte2;
		address[3] = (byte) byte3;
		return address;
	}
	
	public int getByte1() {
		return byte1;
	}
	
	public int getByte2() {
		return byte2;
	}
	
	public int getByte3() {
		return byte3;
	}
	
	public String toString() {
		return String.format("%02X",byte1)+":"+String.format("%02X",byte2)+":"+String.format("%02X",byte3);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + byte1;
		result = prime * result + byte2;
		result = prime * result + byte3;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		InsteonAddress other = (InsteonAddress) obj;
		if (byte1 != other.byte1)
			return false;
		if (byte2 != other.byte2)
			return false;
		if (byte3 != other.byte3)
			return false;
		return true;
	}
	
	
}
